//
//  BluetoothDelegate.swift
//  Nexs
//
//  Created by roni on 2025/7/28.
//

import Foundation
import CoreBluetooth

// MARK: - 蓝牙管理器代理协议
public protocol BluetoothManagerDelegate: AnyObject {

    /// 蓝牙状态发生变化
    /// - Parameter state: 新的连接状态
    func bluetoothManager(_ manager: BluetoothManager, didChangeState state: BluetoothConnectionState)
    
    /// 蓝牙中央管理器状态发生变化
    /// - Parameters:
    ///   - manager: 蓝牙管理器
    ///   - centralState: 中央管理器的新状态
    ///   - message: 状态变化的描述信息
    func bluetoothManager(_ manager: BluetoothManager, didChangeCentralState centralState: CBManagerState, message: String)
    
    /// 发现蓝牙设备
    /// - Parameters:
    ///   - manager: 蓝牙管理器
    ///   - peripheral: 发现的设备
    ///   - advertisementData: 广播数据
    ///   - RSSI: 信号强度
    func bluetoothManager(_ manager: BluetoothManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber)
    
    /// 发现新设备， 发现蓝牙设备满足条件后回调
    /// - Parameters:
    ///   - manager: 蓝牙管理器
    ///   - device: 发现的设备
    func bluetoothManager(_ manager: BluetoothManager, didDiscoverDevice device: BluetoothDevice)
    
    /// 成功连接到设备
    /// - Parameters:
    ///   - manager: 蓝牙管理器
    ///   - device: 连接的设备
    func bluetoothManager(_ manager: BluetoothManager, didConnectToDevice device: BluetoothDevice)
    
    /// 连接失败
    /// - Parameters:
    ///   - manager: 蓝牙管理器
    ///   - device: 尝试连接的设备
    ///   - error: 错误信息
    func bluetoothManager(_ manager: BluetoothManager, didFailToConnect device: BluetoothDevice?, error: BluetoothError)
    
    /// 设备断开连接
    /// - Parameters:
    ///   - manager: 蓝牙管理器
    ///   - device: 断开连接的设备
    ///   - error: 错误信息（如果是意外断开）
    func bluetoothManager(_ manager: BluetoothManager, didDisconnectFromDevice device: BluetoothDevice, error: Error?)
    
    /// 已连接设备列表更新
    /// - Parameters:
    ///   - manager: 蓝牙管理器
    ///   - connectedDevices: 当前已连接的设备列表
    func bluetoothManager(_ manager: BluetoothManager, didUpdateConnectedDevices connectedDevices: [ConnectedBluetoothDevice])
    
    /// 订阅特征值结果
    /// - Parameters:
    ///   - manager: 蓝牙管理器
    ///   - characteristic: 所属特征值
    ///   - isSuccess: 订阅结果
    func bluetoothManager(_ manager: BluetoothManager, didSubscribeCharacteristics characteristic: CBCharacteristic, device: BluetoothDevice?, isSuccess: Bool)
    
    /// 接收到数据， 代理和回调任意接收一个
    /// - Parameters:
    ///   - manager: 蓝牙管理器
    ///   - data: 接收到的数据
    ///   - device:消息设备
    ///   - characteristic: 特征值
    func bluetoothManager(_ manager: BluetoothManager, didReceiveData data: Data, from device: BluetoothDevice, on characteristic: CBCharacteristic)
    
    /// 数据写入完成
    /// - Parameters:
    ///   - manager: 蓝牙管理器
    ///   - characteristic: 特征值
    ///   - error: 错误信息（如果有）
    func bluetoothManager(_ manager: BluetoothManager, didWriteValueFor characteristic: CBCharacteristic, device: BluetoothDevice?, error: Error?)
}

// MARK: - 代理协议默认实现（可选方法）
public extension BluetoothManagerDelegate {
    func bluetoothManager(_ manager: BluetoothManager, didChangeState state: BluetoothConnectionState) {}
    func bluetoothManager(_ manager: BluetoothManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) {}
    func bluetoothManager(_ manager: BluetoothManager, didChangeCentralState centralState: CBManagerState, message: String) {}
    func bluetoothManager(_ manager: BluetoothManager, didDiscoverDevice device: BluetoothDevice) {}
    func bluetoothManager(_ manager: BluetoothManager, didConnectToDevice device: BluetoothDevice) {}
    func bluetoothManager(_ manager: BluetoothManager, didFailToConnect device: BluetoothDevice?, error: BluetoothError) {}
    func bluetoothManager(_ manager: BluetoothManager, didDisconnectFromDevice device: BluetoothDevice, error: Error?) {}
    func bluetoothManager(_ manager: BluetoothManager, didUpdateConnectedDevices connectedDevices: [ConnectedBluetoothDevice]) {}
    func bluetoothManager(_ manager: BluetoothManager, didSubscribeCharacteristics characteristic: CBCharacteristic, device: BluetoothDevice?, isSuccess: Bool) {}
    func bluetoothManager(_ manager: BluetoothManager, didReceiveData data: Data, from device: BluetoothDevice, on characteristic: CBCharacteristic) {}
    func bluetoothManager(_ manager: BluetoothManager, didWriteValueFor characteristic: CBCharacteristic, device: BluetoothDevice?, error: Error?) {}
}

// MARK: - 回调闭包类型定义
public typealias BluetoothScanCompletion = (Result<[BluetoothDevice], BluetoothError>) -> Void
public typealias BluetoothConnectionCompletion = (Result<BluetoothDevice, BluetoothError>) -> Void
public typealias BluetoothServiceDiscoveryCompletion = (Result<[CBService], BluetoothError>) -> Void
public typealias BluetoothCharacteristicDiscoveryCompletion = (Result<[CBCharacteristic], BluetoothError>) -> Void
public typealias BluetoothDataCompletion = (Result<Data, BluetoothError>, CBCharacteristic?) -> Void
public typealias BluetoothWriteCompletion = (Result<Void, BluetoothError>) -> Void
public typealias BluetoothReadRSSICompletion = (Result<NSNumber, BluetoothError>) -> Void
